package com.example.test.controller;

import com.example.test.Bean.UserBean;
import com.example.test.Bean.UserImgBean;
import com.example.test.service.UserService;
import com.example.test.utils.FileUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 控制层 controller
 */
@Controller
public class UserController {
    //自动装配 bean
    @Autowired
    UserService userservice;

//    @RequestMapping("/users")
//    public String showUser(ModelMap modelmap){
//        List<UserBean> allUser = userservice.getAllUser();
//        modelmap.put("userlist",allUser);
//        return "users";
//    }


     //添加用户
    @RequestMapping("/addUser")
    @ResponseBody
    public Map addUser(UserBean userBean, HttpServletRequest request){
        int flag = userservice.insertUser(userBean);
        UserImgBean userImgBean = new UserImgBean();
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        for (MultipartFile multipartFile : files) {
            if(multipartFile.isEmpty())
                continue;
            byte[] fileBytes = new byte[0];
            try {
                fileBytes = multipartFile.getBytes();
                String originalFilename = multipartFile.getOriginalFilename();
                originalFilename = originalFilename.substring(originalFilename.lastIndexOf("."));
                String fileName = UUID.randomUUID() + originalFilename;
                boolean bRet = FileUtils.uploadFile(fileBytes,"static/upload/"+fileName);
                if (bRet == false)
                    continue;
                userImgBean.setId(0);
                userImgBean.setUser_id(userBean.getId());
                userImgBean.setImg("upload/"+fileName);
                int iFlag = userservice.insertUserImg(userImgBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map<String,Object> map = new HashMap<String,Object>();
            if(flag == 1){
                map.put("msg","增加用户成功！");
                return map;
            }else{
                map.put("msg","增加用户失败！");
                return map;
            }
    }
    //id 删除用户
    @RequestMapping(value = "/deleteUser+{id}")
    public String deletUser(@PathVariable("id") String id,ModelMap modelMap){
        int i = userservice.deleteUser(id);
        List<UserBean> allUser = userservice.getAllUser();
        modelMap.addAttribute("userlist",allUser);
        if(i==1){
            return "users";
        }else{
            return "error";
        }
    }
    //id 查询用户
    @RequestMapping(value = "/user+{id}")
    public String getUserByid(@PathVariable("id") String id,ModelMap modelmap){
        UserBean user = userservice.getUserById(id);
        modelmap.addAttribute("user",user);
        return "userInfo";
    }
    //修改
    @RequestMapping(value = "/modifyUser")
    @ResponseBody
    public Map updateUaer(UserBean user){
        Map<String,Object> map = new HashMap<String,Object>();
        int i = userservice.updateUser(user);
        if(i==1){
            map.put("msg","修改信息成功");
            return map;
        }else {
            map.put("msg","修改信息失败");
            return map;
        }
    }
    //分页
    @RequestMapping("/users")
    public String index(Model model, @RequestParam(defaultValue = "2") Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<UserBean> pageInfo = userservice.selectUser4Page(pageNum,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("userlist",pageInfo.getList());
        return "users";
    }
    //退出登入
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }
}
